package com.retailshop.shopfinace.wapController;

import com.retailshop.shopfinace.common.MessageConstants;
import com.retailshop.shopfinace.common.Response;
import com.retailshop.shopfinace.common.UserLoginToken;
import com.retailshop.shopfinace.entity.*;
import com.retailshop.shopfinace.service.FinaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@RequestMapping("/wap")
@Controller
public class WapController {

    @Autowired
    FinaceService finaceService;

    /**
     * 添加数据
     * @param request
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Add", method = RequestMethod.POST)
    public Response FinaceAdd(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try {
            int row = finaceService.Add(map, request);
            //System.out.println("row:"+row);
            if (row > 0) {
                return new Response(MessageConstants.MSG_SUCCESS, row, row, "", request);
            } else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "添加数据错误", request);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED, null, 0, "运行异常！", request);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getBalance",method = RequestMethod.GET)
    //获取用户余额
    public Response getBalance(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            FinaceBalance finaceBalance = finaceService.GetBalance(map,request);
            if(finaceBalance!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceBalance,"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getBalanceAll",method = RequestMethod.GET)
    //获取用户所有种类余额
    public Response getBalanceAll(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            FinaceBalanceAll finaceBalance = finaceService.GetBalanceAll(map,request);
            if(finaceBalance!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceBalance,"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getBalanceAllJ",method = RequestMethod.GET)
    //获取用户所有券种余额
    public Response getBalanceAllJ(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            FinaceBalanceAllJ finaceBalance = finaceService.GetBalanceAllJ(map,request);
            if(finaceBalance!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceBalance,"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getBalanceAllX",method = RequestMethod.GET)
    //获取用户所有现金余额
    public Response getBalanceAllX(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            FinaceBalanceAllX finaceBalance = finaceService.GetBalanceAllX(map,request);
            if(finaceBalance!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceBalance,"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/SetFinaceOrder",method = RequestMethod.POST)
    //扣除订单积分
    public Response SetFinaceOrder(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            return finaceService.SetFinaceOrder(map,request);

        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/selectFinaceUserList",method = RequestMethod.GET)
    //获取用户所有积分列表
    public Response selectFinaceUserList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            return finaceService.selectFinaceUserList(map,request);

        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_OPERATE_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getEarnings",method = RequestMethod.GET)
    //获取用户收益
    public Response getEarnings(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            FinaceEarnings finaceEarnings = finaceService.getEarnings(map,request);
            if(finaceEarnings!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceEarnings,"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

//    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getEarningsList",method = RequestMethod.GET)
    //获取用户收益
    public Response getUserEarningsListDay(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            List<Finace> finaceList = finaceService.selectEarningsList(map,request);
            if(finaceList!=null)
            {
                return new Response(MessageConstants.MSG_SUCCESS,finaceList,finaceList.size(),"成功",request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

    @UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/transfer",method = RequestMethod.POST)
    public Response transfer(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{

            int count = finaceService.insertTransfer(map,request);
            if(count>0)
            {
                return new Response(MessageConstants.MSG_SUCCESS,1,1,"转账成功",request);
            }
            else if(count==-1)
            {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "余额不足！", request);
            }
            else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

    //@UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @RequestMapping(value = "/getWeChatList",method = RequestMethod.GET)
    public Response getWeChatList(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{
            int userNoId = map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
            String remark= URLDecoder.decode(map.get("remark") == null ? "" : map.get("remark"),"UTF-8");
            System.out.println("userNoId:"+userNoId+" remark:"+remark);
            List<Finace> finaceList = finaceService.selectWeChatList(userNoId,remark);
            if(finaceList!=null){
                return new Response(MessageConstants.MSG_SUCCESS,finaceList,finaceList.size(),"成功",request);
            }else {
                return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "获取失败！", request);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }

    //@UserLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ResponseBody
    @Transactional
    @RequestMapping(value = "/refund",method = RequestMethod.GET)
    public Response refund(@RequestParam Map<String,String> map, HttpServletRequest request) {
        try{
            double inter=map.get("inter") == null ? 0 : Double.parseDouble(map.get("inter"));
            double costInter=map.get("costInter") == null ? 0 : Double.parseDouble(map.get("costInter"));
            double cashCoupon=map.get("cashCoupon") == null ? 0 : Double.parseDouble(map.get("cashCoupon"));
            double coupon=map.get("coupon") == null ? 0 : Double.parseDouble(map.get("coupon"));
            int userNoId=map.get("userNoId") == null ? 0 : Integer.parseInt(map.get("userNoId"));
            //double freight=map.get("freight") == null ? 0 : Double.parseDouble(map.get("freight"));
            //double cash=order.getCash()==null?0:order.getCash();
            String orderNo=map.get("orderNo") == null ? "" : map.get("orderNo");
            com.retailshop.shopfinace.entity.Order order=new Order();
            order.setInter(inter);
            order.setCostInter(costInter);
            order.setCashCoupon(cashCoupon);
            order.setCoupon(coupon);
            order.setUsernoId(userNoId);
            order.setOrderNo(orderNo);
            /**
             * type 类型
             * 增加:101-199[101后台充值,102在线充值,103转账加]
             * 分享赚161 * 消费赠162 * 运营部163 * 运营商164 * 随机红包165
             * 店主红包166 * 店主差价181 * 芦荟屋差价182 * 合开差价183 * 供货商191
             * 减少:201-299[201提现,203转账减,211支付]
             */
            //System.out.println("userNoId:"+userNoId+" orderNo:"+orderNo);
            List<Finace> finaceList1 = finaceService.selectWeChatListType(1,null,orderNo);
            List<Finace> finaceList2 = finaceService.selectWeChatListType(2,userNoId,orderNo);
            List<Finace> finaceList3 = finaceService.selectWeChatListType(3,userNoId,orderNo);
            List<Finace> finaceList4 = finaceService.selectWeChatListType(4,userNoId,orderNo);
//            double amount1=0;
//            double amount2=0;
//            double amount3=0;
//            double amount4=0;
//            for (Finace finace:finaceList1) {
//                amount1 +=finace.getAmount()==null?0:finace.getAmount();
//            }
//            for (Finace finace:finaceList2) {
//                amount2 +=finace.getAmount()==null?0:finace.getAmount();
//            }
//            for (Finace finace:finaceList3) {
//                amount3 +=finace.getAmount()==null?0:finace.getAmount();
//            }
//            for (Finace finace:finaceList4) {
//                amount4 +=finace.getAmount()==null?0:finace.getAmount();
//            }
//            order.setAmountAll1(amount1);
//            order.setAmountAll2(amount2);
//            order.setAmountAll3(amount3);
//            order.setAmountAll4(amount4);
            Response response= finaceService.refund(finaceList1,finaceList2,finaceList3,finaceList4,order);
            System.out.println(response.toString());
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_REGISTER_FAILED,null,0,"运行异常！",request);
        }
    }
}
